(async () => {
  //导入 db
  const db = require("./db/connect");
  const cors = require('cors');
  //导入配置
  const { PORT } = require("./config");
  //导入路由
  const userRouter = require("./routers/userRouter");
  const loginRouter = require("./routers/loginRouter");
  const checkTokenMiddleware = require("./middlewares/checkTokenMiddleware");

  try {
    //连接数据库
    await db;
    console.log("数据库连接成功");
    // 搭建 http 服务    express  √    http    json-server
    const express = require("express");
    //创建应用对象
    const app = express();
    app.use(cors());
    //设置静态资源文件夹  网站根目录
    app.use(express.static('./public'));
    //使用路由器中间件
    app.use("/admin", loginRouter); // loginRouter 要放到前面才能生效
    app.use("/admin", checkTokenMiddleware, userRouter); //   /admin/user   /admin/user/1002121

    //声明路由规则
    app.get("/", (req, res) => {
      res.send("<h2>项目的接口服务!!!</h2>");
    });

    //监听端口  启动服务
    app.listen(PORT, () => {
      console.log(`服务已经启动, 端口为 ${PORT}`);
    });
  } catch (e) {
    console.log(e);
    console.log("失败~~");
  }
})();
